package com.iwhalecloud.bss.kite.cucc.web.controller.order;

import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.cucc.client.api.inst.IAdditionalOfferInstService;
import com.iwhalecloud.bss.kite.cucc.client.api.order.ITransferPlanService;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.TransferPlanVO;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.dataservice.cache.DcPublicCache;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.manager.transaction.KiteTransactional;
import com.iwhalecloud.bss.kite.web.context.KiteWebContext;
import com.iwhalecloud.bss.kite.web.service.scene.ISceneService;
import com.ztesoft.bss.common.util.ContextUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/transferPlan")
@Api(tags = {
    "/transferPlan - 传输计划"
})
public class TransferPlanController {

    @Autowired
    private ITransferPlanService transferPlanService;

    @Autowired
    private ISceneService sceneService;
    
    @Autowired
    private IAdditionalOfferInstService additionalOfferInstService;
    
    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @KiteTransactional
    @ApiOperation(value = "查询传输计划", notes = "查询传输计划")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "sceneInstId", value = "场景标识", required = true)
    })
    @GetMapping("/getTransferPlan")
    public DataResult<List<TransferPlanVO>> getTransferPlanInfo(@RequestParam String sceneInstId) {
        return ErrorConsts.SUCCESS.getResult(transferPlanService.getTransferPlanInfo(sceneInstId));
    }

    @KiteTransactional
    @ApiOperation(value = "新增传输计划", notes = "新增传输计划")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "sceneInstId", value = "场景标识", required = true),
        @ApiImplicitParam(name = "offerInstId", value = "实例标识", required = true),
        @ApiImplicitParam(name = "roleId", value = "角色标识", required = true)
    })
    @PutMapping("/addTransferPlan")
    public DataResult<TransferPlanVO> addTransferPlanInfo(@RequestParam String sceneInstId,
        @RequestParam String offerInstId, @RequestParam String roleId) {
        String custId = KiteWebContext.getContext().getCustId();
        return ErrorConsts.SUCCESS.getResult(
            transferPlanService.addTransferPlanInfo(sceneInstId, custId, offerInstId, roleId, ""));
    }

    @KiteTransactional
    @ApiOperation(value = "更新传输计划", notes = "更新传输计划")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "sceneInstId", value = "场景标识", required = true),
        @ApiImplicitParam(name = "transferPlanInstId", value = "传输计划实例标识", required = true),
        @ApiImplicitParam(name = "fieldName", value = "产品属性规格编码", required = true),
        @ApiImplicitParam(name = "newValue", value = "产品属性值", required = true),
        @ApiImplicitParam(name = "valueDesc", value = "显示值", required = true),
        @ApiImplicitParam(name = "extJson", value = "", required = false)
    })
    @PutMapping("/updateTransferPlan")
    public DataResult<TransferPlanVO> updateTransferPlanInfo(@RequestParam String sceneInstId,
        @RequestParam String transferPlanInstId, String fieldName, String newValue, String valueDesc, String extJson) {
        String custId = KiteWebContext.getContext().getCustId();
        String staffId = ContextUtil.getUserId().toString();
        return ErrorConsts.SUCCESS.getResult(transferPlanService.updateTransferPlanInfo(sceneInstId, transferPlanInstId,
            fieldName, newValue, valueDesc, custId, staffId, extJson));
    }

    @KiteTransactional
    @ApiOperation(value = "删除传输计划", notes = "删除传输计划")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "sceneInstId", value = "场景标识", required = true),
        @ApiImplicitParam(name = "transferPlanInstId", value = "传输计划实例标识", required = true)
    })
    @PutMapping("/deleteTransferPlan")
    public DataResult<Boolean> deleteTransferPlanInfo(@RequestParam String sceneInstId, @RequestParam String transferPlanInstId) {
        String custId = KiteWebContext.getContext().getCustId();
        sceneService.deleteProdRole(transferPlanInstId, sceneInstId, custId);
        this.initAdditionalOffer(sceneInstId);
        return ErrorConsts.SUCCESS.getResult(true);
    }
    
    /***
     * 初始化可选资费包
     * @param serviceOfferId
     */
    private void initAdditionalOffer(String sceneInstId) {
        String serviceOfferId = this.querySceneInstService.getSceneInst(sceneInstId).getServiceOfferId();
        //主商品实例ID
        String  offerInstId = this.querySceneInstService.getExtParameterValue(KiteWebContext.getContext().getCurrentSceneInstId(), "mainOfferInstId");
        List<KiteProdInst> prodInsts = querySceneInstService.listProdInstsFromOfferInst(KiteWebContext.getContext().getCustId(), offerInstId);

        if (DcPublicCache.getByPkey(LocalKeyConsts.ADD_OFFER_STYPE, serviceOfferId) != null) {
            DcPublicCache.getByPkey(LocalKeyConsts.ADD_OFFER_STYPE, serviceOfferId).stream().forEach(dcPublic -> {
                additionalOfferInstService.addAdditionalOffer(KiteWebContext.getContext().getCurrentSceneInstId(), prodInsts.get(0).getProdInstId(), offerInstId, dcPublic.getPcode(), KiteWebContext.getContext().getCustId(),
                    "1", true);
            });
        }
    }
}
